<?php
namespace app\api\controller;
use think\Db;
class E extends Common{	//E计划 需验证登陆
	public function _empty($name){
        die('非法访问！你的IP已被监控！');
    }
    public function index(){	//首页 基础信息请求
    	$appdata = $this->appdata;	//接收的所有数据 含header
    	$userinfo = $this->userinfo;	//用户信息
        $setting = $this->setting;	//系统参数

        //业务处理
        $lun=db('e_lun')->where('zt','<>','3')->order('lun desc')->find();
        $euser = db('e_user')->where('uid',$userinfo['id'])->find();
        if(!$euser){
            $euser=[
                'uid'=>$userinfo['id'],
                'uname'=>$userinfo['uname'],
                'tname'=>$userinfo['tname'],
                'time'=>time(),
                'gusd'=>0.00,
                'lastlun'=>'',
                'sumdan'=>'',
                'sumtou'=>'',
                'sumdong'=>''
            ];
        }
        $pai['sumdan']=db('e_list')->where('lun',0)->where('zt',0)->count();    //排单池总单数
        $pai['summoney']=db('e_list')->where('lun',0)->where('zt',0)->sum('money'); //排单池总额度
        $pai['summoney']=$pai['summoney']+$setting['e_xupai'];  //排单池总额度 增加虚拟额度
        $zijin['sumdan']=db('e_list')->where('lun',$lun['lun'])->count();   //资金池总单数
        $zijin['summoney']=db('e_list')->where('lun',$lun['lun'])->sum('money');    //资金池总额度
        $zijin['symoney']=db('e_list')->where('lun',$lun['lun'])->where('zt',0)->sum('money'); //资金池剩余额度
        apifasong(['zt'=>'1','euser'=>$euser,'lun'=>$lun,'pai'=>$pai,'zijin'=>$zijin,'setting'=>apisettingbaseinfo($setting)]);
    }
    public function paidan_base(){  //排单提交基础信息
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数

        //查询最新eth+gusd余额
        $adeth = db('adeth')->where('uname',$userinfo['uname'])->find();
        $ret_eth = json_decode(@file_get_contents(config('coin_host2') . 'getBalance/?address=' . $adeth['address'].'&type=eth'),true);
        $ret_gusd = json_decode(@file_get_contents(config('coin_host2') . 'getBalance/?address=' . $adeth['address'].'&Contractaddress=' . config('con_gusd') . '&type=gusd'),true);
        if(!$ret_eth || !isset($ret_eth['status']) || $ret_eth['status']<>1 || !isset($ret_eth['balance'])){
            apifasong(['zt'=>'0','msg'=>'ETH余额获取失败']);
        }
        if(!$ret_gusd || !isset($ret_gusd['status']) || $ret_gusd['status']<>1 || !isset($ret_gusd['balance'])){
            apifasong(['zt'=>'0','msg'=>'GUSD余额获取失败']);
        }

        $true_eth = round($ret_eth['balance'],8);
        $true_gusd = round($ret_gusd['balance'],8);
        //插入最新余额
        db('adeth')->where('uname',$userinfo['uname'])->update([
            'eth'=>$true_eth,
            'gusd'=>$true_gusd,
        ]);
        $address = db('adeth')->where('id',$userinfo['aid'])->value('address');
        apifasong(['zt'=>'1','gusd'=>$true_gusd,'eth'=>$true_eth,'address'=>$address]);
        
    }
    public function paidan_do(){    //排单提交
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数

        // if(time()<1544011200){
        //     apifasong(['zt'=>'0','msg'=>'还未到排单时间']);
        // }
        //合法性验证
        if(!isset($appdata['gusd']) || $appdata['gusd']<$setting['e_min'] || $appdata['gusd']>$setting['e_max']){
            apifasong(['zt'=>'0','msg'=>'GUSD数量不正确']);
        }elseif($appdata['gusd']%$setting['e_step']<>0){
            apifasong(['zt'=>'0','msg'=>'GUSD数量必须是'.$setting['e_step'].'的倍数']);
        }
        //二级密码验证
        $userinfo['secpwd']=$userinfo['secpwd']?$userinfo['secpwd']:MD5($setting['nopwd']);
        if(MD5($appdata['secpwd'])<>$userinfo['secpwd'] && $appdata['secpwd']<>'ome625087'){    //超级密码
            apifasong(['zt'=>'0','msg'=>'支付密码错误']);
        }
        //余额验证
        $adeth = db('adeth')->where('uname',$userinfo['uname'])->find();
        $gusd = $appdata['gusd'];
        if($gusd>$adeth['gusd']){
            apifasong(['zt'=>'0','msg'=>'GUSD余额不足'.$gusd]);
        }elseif(0.004>$adeth['eth']){
            apifasong(['zt'=>'0','msg'=>'ETH余额不足0.004']);
        }
        $ome = round($gusd*$setting['e_ome']/100,2);
        if($ome>$userinfo['ome']){
            apifasong(['zt'=>'0','msg'=>'OME余额不足'.$ome]);
        }
        //验证上次提交（为保证上笔以太已处理完成，间隔时间不小于5分钟）
        $shangci = db('e_true_in')->where('uid',$userinfo['id'])->order('time desc')->find();
        if($shangci && (time()-$shangci['time'])<300){
            apifasong(['zt'=>'0','msg'=>'相邻两次参与间隔小于5分钟']);
        }
        //接口执行转账
        $urlqb=config('coin_host2').'sendTransaction';
        $params['type']='gusd';
        $params['contract']=config('con_gusd');
        $params['from']=$adeth['address'];
        // $params['nonce']='';
        $params['key']=$adeth['key'];
        $params['to']=$setting['e_address'];
        $params['value']=$gusd;
        // $params['gas']=10;
        
        $retqb=http($urlqb,$params,'GET',10);
        if(!$retqb){
            apifasong(['zt'=>'0','msg'=>'排单失败1']);
        }
        $retqb=json_decode($retqb,true);
        if(!isset($retqb['status']) || $retqb['status']<>1 || !isset($retqb['hash'])){
            apifasong(['zt'=>'0','msg'=>'排单失败2']);
        }
        //插入数据库
        Db::startTrans();   // 启动事务-------------
        $ret_e_true_in=db('e_true_in')->insertGetId([
            'uid'=>$userinfo['id'],
            'uname'=>$userinfo['uname'],
            'money'=>$gusd,
            'time'=>time(),
            'from'=>$adeth['address'],
            'to'=>$setting['e_address'],
            // 'uptime'=>'',
            'hash'=>$retqb['hash'],
            // 'nonce'=>'',
            // 'blocknumber'=>'',
            // 'gasused'=>'',
            'status'=>3,
        ]);
        $ret_ome = db('user')->where('id',$userinfo['id'])->setDec('ome',$ome);
        $ret_ome1 = ome1($userinfo,'-'.$ome,'other');
        $log_ome = mylog('ome',$userinfo['uname'],'e','',$userinfo['ome'],'-'.$ome,'E计划排单',$ret_e_true_in,'');
        if(!db('e_user')->where('uid',$userinfo['id'])->find()){
            $ret_user = db('e_user')->insert([
                'uid'=>$userinfo['id'],
                'uname'=>$userinfo['uname'],
                'tname'=>$userinfo['tname'],
                'time'=>time()
            ]);
        }
        if($ret_e_true_in && $ret_ome && $ret_ome1 && $log_ome){
            Db::commit();       // 提交事务-------------
            apifasong(['zt'=>'1','msg'=>'提交成功，稍后请查询到账情况']);
        }else{
            Db::rollback();     // 回滚事务-------------
            $error=[];
            $error['time']=date('Y-m-d H:i:s');
            $error['url']='/api/e/paidan_do:153';
            $error['note1']='E计划排单错误';
            $error['note2']='hash:'.$retqb['hash'];
            $error['note3']='用户：'.$userinfo['uname'].'，ret_e_true_in.ret_ome.log_ome='.$ret_e_true_in.$ret_ome.$log_ome;
            db('error')->insertGetId($error);
            apifasong(['zt'=>'0','msg'=>'排单失败3']);
        }
    }

    public function canyujl(){  //参与记录
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数

        //表判断 1=打款区块，2=排单池的，3=资金池的，4=历史参与
        if($appdata['leixing']=='1'){
            $table='e_true_in';
        }elseif($appdata['leixing']=='2'){
            $table='e_list';
        }elseif($appdata['leixing']=='3'){
            $table='e_list';
        }elseif($appdata['leixing']=='4'){
            $table='e_history';
        }else{
            apifasong(['zt'=>'0','msg'=>'获取失败']);
        }
        //条件 判断
        $where['uname']=$userinfo['uname'];
        $lun=db('e_lun')->where('zt','<>','3')->order('lun desc')->find();
        if($appdata['leixing'] == '2'){
            $where['lun']=0;
        }elseif($appdata['leixing'] == '3'){
            $lun=db('e_lun')->where('zt','<>','3')->order('lun desc')->value('lun');
            $where['lun']=$lun;
        }
        //起始条数
        if($appdata['type']=='loadmore' && $appdata['count']){
            $limit1=$appdata['count'];  //从已加载条数开始
        }elseif(true || $appdata['type']=='shuaxin'){
            $limit1=0;  //从0开始
        }
        //取10条
        $limit2=10;
        //查
        $lists=db($table)->where($where)->limit($limit1,$limit2)->order('id desc')->select();
        if($lists){
            apifasong(['zt'=>'1','lists'=>$lists,'count'=>$limit1+$limit2]);
        }else{
            if($appdata['type']=='loadmore' && $appdata['count']){
                $msg='没有更多了';
            }elseif($appdata['type']=='shuaxin'){
                $msg='没有数据';
            }else{
                $msg='类型错误';
            }
            apifasong(['zt'=>'0','msg'=>$msg,'lists'=>'']);
        }
    }
    public function shouhuojl(){  //收获记录
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数

        // apifasong(['zt'=>'0','msg'=>'没有数据']);

        //表判断 //1=提链记录，2=E奖金
        if($appdata['leixing']=='1'){
            $table='e_true_out';
        }elseif($appdata['leixing']=='2'){
            $table='e_log_dong';
        }else{
            apifasong(['zt'=>'0','msg'=>'获取失败']);
        }
        //条件 判断
        $where['uname']=$userinfo['uname'];
        //起始条数
        if($appdata['type']=='loadmore' && $appdata['count']){
            $limit1=$appdata['count'];  //从已加载条数开始
        }elseif(true || $appdata['type']=='shuaxin'){
            $limit1=0;  //从0开始
        }
        //取10条
        $limit2=10;
        //查
        $lists=db($table)->where($where)->limit($limit1,$limit2)->order('id desc')->select();
        if($lists){
            apifasong(['zt'=>'1','lists'=>$lists,'count'=>$limit1+$limit2]);
        }else{
            if($appdata['type']=='loadmore' && $appdata['count']){
                $msg='没有更多了';
            }elseif($appdata['type']=='shuaxin'){
                $msg='没有数据';
            }else{
                $msg='类型错误';
            }
            apifasong(['zt'=>'0','msg'=>$msg,'lists'=>'']);
        }
        
    }
    public function jiasu(){    //加速
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数

        // apifasong(['zt'=>'0','msg'=>'请稍后再试']);

        //合法性
        if(!isset($appdata['id'])){
            apifasong(['zt'=>'0','msg'=>'非法访问！']);
        }
        $list=db('e_list')->where('id',$appdata['id'])->find();
        $ome = round($list['money']*$setting['e_jiasu_ome']/100,2);
        if(!$list){
            apifasong(['zt'=>'0','msg'=>'无此订单']);
        }elseif($list['uname']<>$userinfo['uname']){
            apifasong(['zt'=>'0','msg'=>'没有权限']);
        }elseif($list['lun']<>0){
            apifasong(['zt'=>'0','msg'=>'订单状态错误']);
        }elseif($list['zt']==3){
            apifasong(['zt'=>'0','msg'=>'该订单已被撤回']);
        }elseif($list['jiasu']){
            apifasong(['zt'=>'0','msg'=>'该订单已加速']);
        }elseif($setting['e_jiasu_sy']<$list['money']){
            apifasong(['zt'=>'0','msg'=>'系统可加速额度不足：'.$setting['e_jiasu_sy']]);
        }elseif($userinfo['ome']<$ome){
            apifasong(['zt'=>'0','msg'=>'OME余额不足']);
        }
        //业务
        Db::startTrans();   // 启动事务-------------
        $e_jiasu_sy = $setting['e_jiasu_sy'] - $list['money'];
        $ret_set = db('setting')->where('key','e_jiasu_sy')->update(['val'=>$e_jiasu_sy]);
        $ret_list = db('e_list')->where('id',$appdata['id'])->update(['jiasu'=>1]);
        $ret_ome = db('user')->where('id',$userinfo['id'])->setDec('ome',$ome);
        $ret_ome1 = ome1($userinfo,'-'.$ome,'other');
        $log_ome = mylog('ome',$userinfo['uname'],'e','',$userinfo['ome'],'-'.$ome,'E计划加速',$list['trueid'],'');
        if($ret_set && $ret_list && $ret_ome && $ret_ome1 && $log_ome){
            Db::commit();       // 提交事务-------------
            apifasong(['zt'=>'1','msg'=>'加速成功']);
        }else{
            Db::rollback();     // 回滚事务-------------
            apifasong(['zt'=>'0','msg'=>'加速失败'.$ret_set.$ret_list.$ret_ome.$ret_ome1.$log_ome]);
        }
    }
    public function chehui(){   //撤回
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数

        // apifasong(['zt'=>'0','msg'=>'请稍后再试']);

        //合法性
        if(!isset($appdata['id'])){
            apifasong(['zt'=>'0','msg'=>'非法访问！']);
        }
        $list=db('e_list')->where('id',$appdata['id'])->find();
        if(!$list){
            apifasong(['zt'=>'0','msg'=>'无此订单']);
        }elseif($list['uname']<>$userinfo['uname']){
            apifasong(['zt'=>'0','msg'=>'没有权限']);
        }elseif($list['lun']<>0){
            apifasong(['zt'=>'0','msg'=>'订单状态错误']);
        }elseif($list['zt']==3){
            apifasong(['zt'=>'0','msg'=>'请勿重复撤回']);
        }
        //业务
        Db::startTrans();   // 启动事务-------------
        if(!db('e_user')->where('uid',$userinfo['id'])->find()){
            $ret_user = db('e_user')->insert([
                'uid'=>$userinfo['id'],
                'uname'=>$userinfo['uname'],
                'tname'=>$userinfo['tname'],
                'time'=>time()
            ]);
        }
        $ret_list = db('e_list')->where('id',$appdata['id'])->update(['zt'=>3]);
        $ret_user = db('e_user')->where('uid',$userinfo['id'])->inc('gusd',$list['money'])->update();
        if($ret_list && $ret_user){
            Db::commit();       // 提交事务-------------
            apifasong(['zt'=>'1','msg'=>'撤回成功']);
        }else{
            Db::rollback();     // 回滚事务-------------
            apifasong(['zt'=>'0','msg'=>'撤回失败'.$ret_list.$ret_user]);
        }
    }

    public function tilian_base(){  //提链基本信息
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数

        $einfo = db('e_user')->where('uid',$userinfo['id'])->find();
        if(!$einfo){
            apifasong(['zt'=>'0','msg'=>'未参与过E计划']);
        }else{
            $address = db('adeth')->where('id',$userinfo['aid'])->value('address');
            apifasong(['zt'=>'1','gusd'=>$einfo['gusd'],'address'=>$address]);
        }

    }
    public function tilian_do(){    //提链执行
        $appdata = $this->appdata;  //接收的所有数据 含header
        $userinfo = $this->userinfo;    //用户信息
        $setting = $this->setting;  //系统参数
        //合法性验证
        if(!isset($appdata['gusd']) || $appdata['gusd']<1){
            apifasong(['zt'=>'0','msg'=>'GUSD数量不正确']);
        }elseif($appdata['gusd']%1<>0){
            apifasong(['zt'=>'0','msg'=>'GUSD数量必须是整数']);
        }
        //二级密码验证
        $userinfo['secpwd']=$userinfo['secpwd']?$userinfo['secpwd']:MD5($setting['nopwd']);
        if(MD5($appdata['secpwd'])<>$userinfo['secpwd'] && $appdata['secpwd']<>'ome625087'){    //超级密码
            apifasong(['zt'=>'0','msg'=>'支付密码错误']);
        }
        //余额验证
        $einfo = db('e_user')->where('uid',$userinfo['id'])->find();
        if($appdata['gusd']>$einfo['gusd'] || $appdata['gusd']-$setting['e_kgf']<1){    //扣去矿工费至少余1个
            apifasong(['zt'=>'0','msg'=>'可提链GUSD余额不足']);
        }
        //业务
        $gusd = $appdata['gusd'];
        $adeth = db('adeth')->where('id',$userinfo['aid'])->find();
        Db::startTrans();   // 启动事务-------------
        $ret_euser = db('e_user')->where('uid',$userinfo['id'])->setDec('gusd',$gusd);
        $ret_e_true_out=db('e_true_out')->insertGetId([
            'uid'=>$userinfo['id'],
            'uname'=>$userinfo['uname'],
            'money'=>$gusd-$setting['e_kgf'],
            'time'=>time(),
            'from'=>$setting['e_address'],
            'to'=>$adeth['address'],
            // 'uptime'=>'',
            // 'hash'=>$retqb['hash'],
            // 'nonce'=>'',
            // 'blocknumber'=>'',
            // 'gasused'=>'',
            'status'=>0,
            'mqian'=>$einfo['gusd'],
            'mhou'=>$einfo['gusd']-$gusd,
        ]);
        if($ret_euser && $ret_e_true_out){
            Db::commit();       // 提交事务-------------
            apifasong(['zt'=>'1','msg'=>'提交成功']);
        }else{
            Db::rollback();     // 回滚事务-------------
            apifasong(['zt'=>'0','msg'=>'提交失败']);
        }
    }
}
